package com.qianfeng.missfood.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.missfood.R;
import com.qianfeng.missfood.constants.AppConfig;
import com.qianfeng.missfood.db.DBHelper;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText phoneNumber;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        ImageView pressBack = (ImageView) findViewById(R.id.pressBack);
        pressBack.setOnClickListener(this);

        phoneNumber = (EditText) findViewById(R.id.phoneNumber);

        Button registerNext = (Button) findViewById(R.id.registerNext);
        registerNext.setOnClickListener(this);

        TextView serviceRules = (TextView) findViewById(R.id.serviceRules);
        serviceRules.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.pressBack:

                onBackPressed();
                break;
            case R.id.registerNext:

                phone = phoneNumber.getText().toString().trim();
                if ("".equals(phone)) {

                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();

                } else if (!phone.matches("[1-9][0-9]{10}")) {

                    Toast.makeText(this, "请输入正确的号码", Toast.LENGTH_SHORT).show();
                } else {
                    boolean isExit = DBHelper.DbUser.selectByPhone(phone);
                    if (isExit) {

                        Toast.makeText(this, "手机号已存在，是否登录", Toast.LENGTH_SHORT).show();

                    } else {

                        Intent intent = new Intent(this, SmsActivity.class);
                        intent.putExtra("phone", phone);
                        startActivity(intent);

                    }

                }

                break;

            case R.id.serviceRules:

                Intent intent = new Intent(this, ServiceRulesActivity.class);
                startActivity(intent);
                break;

            default:

                break;

        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        setResult(AppConfig.REGISTER_RESULT);
    }
}
